#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
    reload(sys)
    sys.setdefaultencoding(defaultencoding)

from selenium import webdriver
import time

# 打开浏览器和打榜网址
brower = webdriver.Firefox()
brower.get("http://m.so.com/mhtml/zt/hotlist.html")
# 切换到女星榜
brower.find_element_by_xpath("/html/body/div/div[2]/div[2]/div[1]/ul/li[2]").click()

# 向下滑动页面,加载完所有的明星
for i in range(6):
    brower.execute_script("window.scrollTo(0,document.body.scrollHeight)")
    time.sleep(1)

# 找到打榜列表, 把每一个人分开
text = brower.find_element_by_xpath("/html/body/div/div[2]/div[2]/div[3]/ul").get_attribute('innerHTML')
names = text.split("<li class")

# 找到自己爱豆的排名
counter = 0
for i in range(len(names)):
    if "爱豆名字" in names[i]:
        counter = i
        break

# 一次只能打榜5次
for i in range(5):
    brower.find_element_by_xpath("/html/body/div/div[2]/div[2]/div[3]/ul/li["+bytes(counter)+"]/div[3]/span/a").click()
    time.sleep(1)

brower.close()
